var util = require('../../util/util.js')
var app = getApp()
var scrollLoadNum = app.globalData.g_scrollLoadNum
Page({
  data:{
    url:'',
    scrollTotalNum:0,
    total:0,
    movies:{}
  },
  onLoad:function(options){
    var that = this
    var title = util.convertMovieType(options.movieType)
    var url = util.convertMovieType(options.movieType,'url')
    this.setData({
      'solgan':title,
      'url':url
    })
    util.getDoubanMsg(url+'?start=0&count='+scrollLoadNum,function(data){
      that.setData({
        'total':data.total
      })
      var movies = util.resolveDoubanMsg(data)
      that.setData({
        movies:movies,
        'scrollTotalNum':scrollLoadNum
      })
    })
  },
  onReady:function(){
    var that = this
    wx.setNavigationBarTitle({
      title:this.data.solgan,
      success: function(res) {
      }
    })
  },
  //加载更多的方法
   onReachBottom: function () {
    var that = this
    var url = this.data.url
    var scrollTotalNum = this.data.scrollTotalNum
    var total = this.data.total
    if (scrollTotalNum < total - 1) {
      //显示加载动画
      wx.showNavigationBarLoading()
      util.getDoubanMsg(url + '?start=' + scrollTotalNum + '&count=' + scrollLoadNum, function (data) {
        var movies = util.resolveDoubanMsg(data)
        var pre_movies = that.data.movies
        movies = pre_movies.concat(movies)
        //隐藏加载动画
        wx.hideNavigationBarLoading()
        that.setData({
          movies: movies,
          'scrollTotalNum': scrollTotalNum + scrollLoadNum
        })
      })
    }
  },
  //跳转到电影详情页
  forwardDetail:function(event){
    var movieId = event.currentTarget.dataset.id
        wx.navigateTo({
          url: '../movie-detail/movie-detail?id='+movieId,
          success: function(res){
            // success
          },
          fail: function() {
            // fail
          },
          complete: function() {
            // complete
          }
        })
  },
  //页面下拉刷新的方法
  onPullDownRefresh: function () {
    var url = this.data.url
    var that = this
    wx.showNavigationBarLoading()
    util.getDoubanMsg(url + '?start=0&count=' + scrollLoadNum, function (data) {
      var movies = util.resolveDoubanMsg(data)
      that.setData({
        movies: movies,
        'scrollTotalNum': scrollLoadNum
      })
      wx.stopPullDownRefresh()
      wx.hideNavigationBarLoading()
    })
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})